package cn.ges.movie.mapper

import cn.ges.movie.dto.MovieQueryDto
import cn.ges.movie.entity.Movie
import com.baomidou.mybatisplus.core.mapper.BaseMapper
import org.apache.ibatis.annotations.Param
import org.apache.ibatis.annotations.Select
import java.math.BigDecimal
import java.time.LocalDate
import java.time.LocalDateTime

/**
 *
 * @author gespent@163.com
 * @date 2021/4/16 16:39
 */
interface MovieMapper:BaseMapper<Movie> {

    @Select(
        "SELECT a.*, b.max_count as maxCount, b.sdId,b.price,b.start_time as startTime, b.`name` AS mhName ," +
                " sqrt((#{longitude} - longitude) * PI() * 6328 * cos((#{latitude} + latitude) / 2 * PI() / 180) / 180" +
                " * ((#{longitude} - longitude) * PI() * 6328 * cos((#{latitude} + latitude) / 2 * PI() / 180) / 180) " +
                "+ (#{latitude} - latitude) * PI() * 6328 / 180 * ((#{latitude} - latitude) * PI() * 6328 / 180)) " +
                "AS distance FROM mv_cinema a RIGHT JOIN" +
                " ( SELECT a.*, b.price ,b.start_time,b.id as sdId FROM mv_movie_hall a " +
                "LEFT JOIN mv_scheduling b ON a.id = b.mh_id " +
                "WHERE b.start_time > #{startTme} AND b.start_time <#{endTme} AND mv_id =#{mvId} ) " +
                "b ON a.id = b.mc_id ORDER BY distance,start_time  \${limit}"
    )
    fun queryMovieInfo(
        @Param("longitude") longitude: BigDecimal,
        @Param("latitude") latitude: BigDecimal,
        @Param("limit") limit: String,
        @Param("startTme") startTme: LocalDateTime,
        @Param("endTme") endTme: LocalDate,
        @Param("mvId") mvId: Long
    ): ArrayList<MovieQueryDto>

    @Select(
        "SELECT count(*)" +
                " FROM mv_cinema a RIGHT JOIN ( SELECT a.*, b.price FROM mv_movie_hall a LEFT JOIN mv_scheduling b ON a.id = b.mh_id " +
                "WHERE b.start_time > #{startTme} AND b.start_time <#{endTme} AND mv_id =#{mvId} ) b ON a.id = b.mc_id"
    )
    fun queryMovieInfoCount(
        @Param("startTme") startTme: LocalDateTime,
        @Param("endTme") endTme: LocalDate,
        @Param("mvId") mvId: Long
    ): Int

}